package com.esri.android.map.ags;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.esri.android.map.TiledLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.a;
import com.esri.core.internal.util.d;
import com.esri.core.io.UserCredentials;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class ArcGISLocalTiledLayer extends TiledLayer {
    static final int b = 200;
    static final int c = 133;

    native long nativeCreateLayer();

    native double[] nativeGetLODScales(long j);

    native String nativeGetLegendJson(long j);

    native int nativeGetSpatialReference(long j);

    native boolean nativeGetThumbnail(long j, Object obj);

    native boolean nativeInitialize(long j, String str);

    public ArcGISLocalTiledLayer(String str) {
        this(str, true);
    }

    public ArcGISLocalTiledLayer(String str, boolean z) {
        super(str, true);
        if (z) {
            initLayer();
        }
    }

    @Override // com.esri.android.map.Layer
    protected long create() {
        return nativeCreateLayer();
    }

    @Override // com.esri.android.map.Layer
    protected void initLayer() {
        if (this.nativeHandle == 0) {
            this.nativeHandle = create();
        }
        if (this.nativeHandle == 0) {
            changeStatus(OnStatusChangedListener.STATUS.fromInt(-1000));
            return;
        }
        String url = getUrl();
        try {
            if (url.startsWith("file://")) {
                url = new URL(getUrl()).getFile();
            }
            if (new File(url).exists() && nativeInitialize(this.nativeHandle, url)) {
                setDefaultSpatialReference(SpatialReference.create(nativeGetSpatialReference(this.nativeHandle)));
                setFullExtent(getFullExtent());
                changeStatus(OnStatusChangedListener.STATUS.INITIALIZED);
                return;
            }
            Log.e(a.a, "url =" + getUrl() + " is invalid path.");
            changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_ARCGIS_TILE_LAYER));
        } catch (Exception e) {
            Log.e(a.a, "url =" + getUrl(), e);
            changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_ARCGIS_TILE_LAYER));
        }
    }

    @Override // com.esri.android.map.Layer
    public void reinitializeLayer(UserCredentials userCredentials) {
        super.reinitializeLayer(userCredentials);
    }

    public void reinitializeLayer(String str) {
        setUrl(str);
        super.reinitializeLayer((UserCredentials) null);
    }

    public List<ArcGISLayerInfo> getLayers() {
        ArrayList arrayList = new ArrayList();
        JsonParser jsonParser = null;
        try {
            File file = new File(new URL(getUrl()).getFile());
            if (file.isDirectory()) {
                File file2 = new File(file.getParentFile().getParentFile().getAbsolutePath() + "/servicedescriptions/mapserver/mapserver.json");
                if (file2.exists()) {
                    jsonParser = d.a((InputStream) new FileInputStream(file2));
                    jsonParser.nextToken();
                    b(jsonParser, arrayList);
                }
            } else {
                String nativeGetLegendJson = nativeGetLegendJson(this.nativeHandle);
                if (nativeGetLegendJson != null && nativeGetLegendJson.length() > 0) {
                    jsonParser = d.c(nativeGetLegendJson);
                    a(jsonParser, arrayList);
                }
            }
        } catch (Exception unused) {
            if (jsonParser != null) {
                try {
                    jsonParser.close();
                } catch (IOException unused2) {
                }
            }
        }
        return arrayList;
    }

    public Bitmap getThumbnail() {
        File file;
        try {
            String url = getUrl();
            if (url.startsWith("file://")) {
                url = new URL(getUrl()).getFile();
            }
            if (new File(url).isDirectory()) {
                String str = file.getParentFile().getParentFile().getAbsolutePath() + "/esriinfo/thumbnail/thumbnail.png";
                if (new File(str).exists()) {
                    return BitmapFactory.decodeFile(str);
                }
                return null;
            } else if (this.nativeHandle != -1) {
                Bitmap createBitmap = Bitmap.createBitmap(200, c, Bitmap.Config.ARGB_8888);
                if (nativeGetThumbnail(this.nativeHandle, createBitmap)) {
                    return createBitmap;
                }
                return null;
            } else {
                return null;
            }
        } catch (Exception e) {
            Log.w(a.a, e.getCause().getMessage());
            return null;
        }
    }

    private static void a(JsonParser jsonParser, List<ArcGISLayerInfo> list) throws Exception {
        if (jsonParser == null || !d.c(jsonParser)) {
            return;
        }
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if ("layers".equals(currentName)) {
                if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        ArcGISLayerInfo fromJson = ArcGISLayerInfo.fromJson(jsonParser);
                        if (fromJson != null) {
                            list.add(fromJson);
                        }
                    }
                }
            } else {
                jsonParser.skipChildren();
            }
        }
    }

    private static void b(JsonParser jsonParser, List<ArcGISLayerInfo> list) throws Exception {
        if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) {
            return;
        }
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if ("resources".equals(currentName)) {
                if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        c(jsonParser, list);
                    }
                }
            } else {
                jsonParser.skipChildren();
            }
        }
    }

    private static void c(JsonParser jsonParser, List<ArcGISLayerInfo> list) throws Exception {
        if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) {
            return;
        }
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if ("contents".equals(currentName)) {
                a(jsonParser, list);
            } else {
                jsonParser.skipChildren();
            }
        }
    }
}
